//
//  ReaderDirectoryViewController.swift
//  SwiftStudy
//
//  Created by chang on 2018/3/16.
//  Copyright © 2018年 chang. All rights reserved.
//

import UIKit
import Alamofire
import SwiftyJSON

class ReaderDirectoryViewController: UIViewController {

    var novelidStr = NSString.init()
    var tableView : UITableView!
    let listArray = NSMutableArray.init()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.defaultBackgroundColor
        self.navigationItem.title = "小说目录"
        
        self.initTableView()
        
        self.requestListData()
    }

    func requestListData() {
        let url = "http://app.robook.com/iosapi/chapterList?more=0&whole=1"
        let parameDic = ["novelId":novelidStr]
        self.view.makeToastActivity(.center)
        Alamofire.request(url, method: .post, parameters: parameDic).responseJSON {
            [weak self] (response) in
            self?.view.hideToastActivity()
            if let responseCC = response.result.value {
                let json = JSON(responseCC)
                let chapterlist = json["chapterlist"].array
                self?.listArray.removeAllObjects()
                self?.listArray.addObjects(from: chapterlist!)
                self?.tableView.reloadData()
            }
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

extension ReaderDirectoryViewController : UITableViewDataSource,UITableViewDelegate {
    
    func initTableView() {
        tableView = UITableView.init(frame: CGRect.init(x: 0, y: self.navgationBarBottom, width: MAINSCREEN_WIDTH, height: MAINSCREEN_HEIGHT-self.navgationBarBottom));
        tableView.backgroundColor = UIColor.defaultBackgroundColor
        tableView.separatorStyle = .none
        tableView.delegate = self
        tableView.dataSource = self
        self.view.addSubview(tableView)
    }
    
    //MARK: --UITableViewDataSource,UITableViewDelegate
    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listArray.count
    }
    
    public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 126
    }
    
    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let str = "cell"
        var cell = tableView.dequeueReusableCell(withIdentifier: str)
        if cell == nil {
            cell = UITableViewCell.init(style: .default, reuseIdentifier: str)
            cell?.backgroundColor = UIColor.defaultBackgroundColor
            cell?.selectionStyle = .none
            
            let bgView = UIView.init(frame: CGRect.init(x: 15, y: 10, width: tableView.width-15*2, height: 126-10*2))
            bgView.tag = 9
            bgView.backgroundColor = UIColorFromRGB(rgbValue: 0x7fffd4)//0xf0f8ff
            bgView.layer.cornerRadius = 10
            bgView.layer.masksToBounds = true
            bgView.layer.borderWidth = 0.5
            bgView.layer.borderColor = UIColor.defaultLineColor.cgColor
            cell?.addSubview(bgView)
            
            let tipLabel = UILabel.init(frame: CGRect.init(x: 12, y: 8, width: bgView.width-24-90-5, height: 20))
            tipLabel.tag = 10
            tipLabel.font = UIFont.systemFont(ofSize: 15)
            tipLabel.textColor = UIColor.defaultTextColor
            bgView.addSubview(tipLabel)
            
            let chapterclickLabel = UILabel.init(frame: CGRect.init(x: bgView.width-12-90, y: tipLabel.top, width: 90, height: tipLabel.height))
            chapterclickLabel.tag = 11
            chapterclickLabel.font = UIFont.systemFont(ofSize: 13)
            chapterclickLabel.textColor = UIColor.defaultRedColor
            chapterclickLabel.textAlignment = .right
            bgView.addSubview(chapterclickLabel)
            
            let timeLabel = UILabel.init(frame: CGRect.init(x: 12, y: tipLabel.bottom+5, width: bgView.width-24, height: 20))
            timeLabel.tag = 12
            timeLabel.font = UIFont.systemFont(ofSize: 12)
            timeLabel.textColor = UIColorFromRGB(rgbValue: 0x556b2f)
            timeLabel.textAlignment = .left
            bgView.addSubview(timeLabel)

            let chapterintroLabel = UILabel.init(frame: CGRect.init(x: 12, y: timeLabel.bottom+5, width: bgView.width-24, height: 20*2))
            chapterintroLabel.tag = 13
            chapterintroLabel.font = UIFont.systemFont(ofSize: 14)
            chapterintroLabel.textColor = UIColor.gray
            chapterintroLabel.numberOfLines = 2
            bgView.addSubview(chapterintroLabel)
        }
        
        if let dic  = JSON(listArray.object(at: indexPath.row)).dictionary {
            let chaptername = (JSON(dic)["chaptername"]).string
            let bgView = cell?.viewWithTag(9)
            let tipLabel = bgView?.viewWithTag(10) as! UILabel
            tipLabel.text = chaptername
            
            let chapterclickLabel = bgView?.viewWithTag(11) as! UILabel
            let chapterclick = (JSON(dic)["chapterclick"]).string
            chapterclickLabel.text = chapterclick
            
            let timeLabel = bgView?.viewWithTag(12) as! UILabel
            let chapterdate = (JSON(dic)["chapterdate"]).string
            let chaptersize = (JSON(dic)["chaptersize"]).string
            timeLabel.text = "\(chapterdate!)" + "       " + "\(chaptersize!)" + "字"
            
            let chapterintroLabel = bgView?.viewWithTag(13) as! UILabel
            let chapterintro = (JSON(dic)["chapterintro"]).string
            chapterintroLabel.text = chapterintro
        }
        
        return cell!
    }
    
    public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let dic = JSON(listArray.object(at: indexPath.row)).dictionary {
            let readerVC = ReaderContentViewController.init()
            self.navigationController?.pushViewController(readerVC, animated: true)
        }
    }
    
}
